import { createFeatureSelector, createSelector, select, Store } from '@ngrx/store';
import { AuthState } from './auth.models';
import { AppState } from '@core/cne.state';
import { AuthUser } from '@shared/models/user.models';
import { take, Observable } from 'rxjs';

// 选择认证状态的特征选择器
export const selectAuthState = createFeatureSelector<AuthState>('auth');

// 选择完整的认证状态
export const selectAuth = createSelector(selectAuthState, (state: AuthState) => state);

// 选择是否已认证
export const selectIsAuthenticated = createSelector(selectAuthState, (state: AuthState) => state.isAuthenticated);

// 选择是否已加载用户
export const selectIsUserLoaded = createSelector(selectAuthState, (state: AuthState) => state.isUserLoaded);

// 选择认证用户
export const selectAuthUser = createSelector(selectAuthState, (state: AuthState) => state.authUser);

// 获取当前认证用户（异步）
export const getCurrentAuthUser = (store: Store<AppState>): Observable<AuthUser | null> => {
  return store.pipe(select(selectAuthUser));
};